<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class UserController extends GxController {
    public $cssFile = null;
	
	public $layout='//layouts/base';
    
	public function filters() {
		return array(
			'accessControl', 
		);
	}
    
    public function accessRules() {
        return array(
			array('allow',
				'actions'=>array('index', 'captcha', 'success', 'activation'),
                'controllers' => array('registration'),
				'users'=>array('?'),
			),
            array('allow',
                'actions'=>array('index','password'),
                'controllers' => array('profile'),
                'users'=>array('@'),
            ),
            array('allow',
                'actions'=>array('index','create', 'update', 'delete', 'uploadimage'),
                'controllers' => array('myfirms'),
                'users'=>array('@'),
            ),           
/*            
            array('deny',
                'users'=>array('*'),
            ),          
*/            
        );
    } 	
    
    
    public static function checkLoggedIn() {
		 return (Yii::app()->user->isGuest) ? false : true;
        //if (!isset(Yii::app()->user)) return false;
        //return (0 < Yii::app()->user->id) ? true : false;
    }
		
    
    public function getBaseUrl() {
        return Yii::app()->getBaseUrl(true);
    }
    
    protected function beforeAction($action) {
        //if ($this->checkLoggedIn()) $this->redirect(Yii::app()->controller->module->homeUrl);
        return parent::beforeAction($action);
    }    
    
    
    
    protected function beforeRender($view) {
        if (!is_null($this->cssFile)) {
            Yii::app()->getClientScript()->registerCssFile($this->cssFile);
        }
        return parent::beforeRender($view);
    }
	
	
	protected function sendActivationEmail($model = null) {
		if (!$model) return false;
		$activation_url = $this->createAbsoluteUrl('/user/registration/activation',array("activkey" => $model->hkey, "email" => $model->email));
		try {
			$mail_path = Yii::app()->mail->viewPath;
			Yii::app()->mail->viewPath = 'application.modules.user.views.registration';

			//sent to user
			$message = new YiiMailMessage;
			$message->view = 'email_activation_user';
			$message->setBody(array('title' => UserModule::t('Активация аккаунта') , 'model'=>$model, 'activation_url' => $activation_url), 'text/html');
			$message->addTo($model->email);
			$message->setSubject(UserModule::t('Активация аккаунта'));
			$message->from = Yii::app()->params['systemEmail'];
			Yii::app()->mail->send($message);

			//sent to special person
			$sysmessage = new YiiMailMessage;
			$sysmessage->view = 'email_activation_admin';
			$sysmessage->setBody(array('title' => UserModule::t('Активация аккаунта'), 'model'=>$model), 'text/html');
			$sysmessage->addTo(Yii::app()->params['adminEmail']);
			$sysmessage->setSubject(UserModule::t('Запрос на активацию: информация об пользователе'));
			$sysmessage->from = Yii::app()->params['systemEmail'];
			Yii::app()->mail->send($sysmessage);  
			Yii::app()->mail->viewPath = $mail_path;
			
		/*
	   $message = new YiiMailMessage;
	   $message->view = 'email';
	   $message->setBody($subst, 'text/plain','utf-8');
	   # get subject
	   $body = $message->message->getBody();
	   $lines = explode("\n",$body);
	   $subject = array_shift($lines);
	   $message->message->setBody(implode("\n",$lines),
	       $message->message->getContentType()
	   );
	   # set, send
	   $message->setSubject($subject);
	   $message->addTo($to);
	   $message->from = Yii::app()->params['adminEmail'];
	   Yii::app()->mail->send($message);
		*/			
			
			
			
			return true;
		} catch (Exception $e) {
			//	$e->getMessage();
			return false;
		}
		return false;		
	}
	
	public function notApprovedPartialContent() {
		return $this->renderPartial('default/notapproved', array());
	}

}